﻿Imports System.IO

Public Class 入货管理

#Region "外观处理"

    '关闭窗体
    Private Sub 入货管理_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        CommoditIn = Nothing
    End Sub

    '窗体大小改变
    Private Sub 入货管理_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        If Me.Width < 459 Then
            Me.Width = 459
        End If
        If Me.Height < 308 Then
            Me.Height = 308
        End If
    End Sub

#End Region


    '窗体加载
    Private Sub 入货管理_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        PictureBox1.ImageLocation = apps + "\skin\nopic.jpg"

        '加载品牌
        Dim sqlstr As String = "select 品牌 from 品牌表"
        Dim ds As Data.DataSet = SQLtoDataSet(sqlstr)
        Dim ds_table As Data.DataTable = ds.Tables(0)
        If ds_table.Rows.Count > 0 Then
            For i = 0 To ds_table.Rows.Count - 1
                ComboBox1.Items.Add(ds_table.Rows(i).Item(0).ToString)
            Next
        End If

        PictureBox1.ImageLocation = apps + "\skin\nopic.jpg"

    End Sub

    '加载型号
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Try
            ComboBox2.Text = "请选择..."

            If ComboBox1.Text = "请选择..." Or ComboBox1.Text = "" Then
                Exit Sub
            End If

            Dim sqlstr As String = "select 型号 from 型号表 where 所属品牌 = '" + ComboBox1.Text + "'"
            Dim ds As Data.DataSet = SQLtoDataSet(sqlstr)
            ComboBox2.Items.Clear()
            Dim ds_table As Data.DataTable = ds.Tables(0)
            If ds_table.Rows.Count > 0 Then
                For i = 0 To ds_table.Rows.Count - 1
                    ComboBox2.Items.Add(ds_table.Rows(i).Item(0).ToString)
                Next
            End If

            'ComboBox2.Focus()
        Catch ex As Exception

        End Try
    End Sub

    '择择型号
    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        Try

            If ComboBox2.Text = "请选择..." Or ComboBox2.Text = "" Then
                Exit Sub
            End If

            Dim sqlstr As String = "select 型号,一般售价 from 型号表 where 所属品牌 = '" + ComboBox1.Text + "' and 型号 ='" + ComboBox2.Text + "'"
            Dim ds As Data.DataSet = SQLtoDataSet(sqlstr)
            Dim ds_table As Data.DataTable = ds.Tables(0)
            If ds_table.Rows.Count > 0 Then
                TextBox1.Text = ds_table.Rows(0).Item("一般售价").ToString
                Dim pic As String = apps + "\PIC\" + ComboBox1.Text + "_" + ComboBox2.Text + ".jpg"
                If File.Exists(pic) Then
                    PictureBox1.ImageLocation = pic
                Else
                    PictureBox1.ImageLocation = apps + "\skin\nopic.jpg"
                End If
            End If

            'TextBox1.Focus()

        Catch ex As Exception

        End Try

    End Sub

    '计算总和
    Private Sub sumprice_celcu(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
        Dim sumprice As Double = Val(TextBox1.Text) * NumericUpDown1.Value
        TextBox2.Text = sumprice.ToString
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        sumprice_celcu(sender, e)
    End Sub
    Private Sub NumericUpDown1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles NumericUpDown1.LostFocus
        sumprice_celcu(sender, e)
    End Sub
    Private Sub NumericUpDown1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles NumericUpDown1.GotFocus
        sumprice_celcu(sender, e)

    End Sub
    Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
        sumprice_celcu(sender, e)
    End Sub
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        sumprice_celcu(sender, e)
    End Sub

    '只能输入数字
    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar >= "0" And e.KeyChar <= "9" Or e.KeyChar = Chr(Keys.Back) Or e.KeyChar = Chr(46) Then  '只允许输入数字与点
            e.Handled = False           '当按键不合法时　则不允许输入
        Else
            e.Handled = True            '当按键合法时　则允许输入并显示
        End If
    End Sub
    Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        If e.KeyChar >= "0" And e.KeyChar <= "9" Or e.KeyChar = Chr(Keys.Back) Or e.KeyChar = Chr(46) Then  '只允许输入数字与点
            e.Handled = False           '当按键不合法时　则不允许输入
        Else
            e.Handled = True            '当按键合法时　则允许输入并显示
        End If
    End Sub

    '入货确定
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim PP As String = ComboBox1.Text
        Dim XH As String = ComboBox2.Text
        Dim SL As String = NumericUpDown1.Value.ToString
        Dim DJ As String = TextBox1.Text
        Dim ZE As String = TextBox2.Text
        Dim BZ As String = TextBox3.Text

        Dim RQ As String = Now.ToString

        If ComboBox1.Text = "请选择..." Or ComboBox1.Text = "" Then
            MsgBox("请选择你要入货的品牌！")
            ComboBox1.Focus()
            Exit Sub
        End If

        If ComboBox2.Text = "请选择..." Or ComboBox2.Text = "" Then
            MsgBox("请选择你要入货的型号！")
            ComboBox2.Focus()
            Exit Sub
        End If

        If Not IsNumeric(DJ) Then
            MsgBox("你输入的入价单价不是一个正确的数字，请重新输入！")
            TextBox1.Focus()
            Exit Sub
        End If

        If Not IsNumeric(ZE) Then
            MsgBox("你输入的总价不是一个正确的数字，请重新输入！")
            TextBox2.Focus()
            Exit Sub
        End If

        Dim sqlstr As String = ""
        sqlstr += "insert into 入货表(品牌,型号,数量,入货单价,合计总额,日期,备注1) values("
        sqlstr += "'" + PP + "',"
        sqlstr += "'" + XH + "',"
        sqlstr += "'" + SL + "',"
        sqlstr += "'" + DJ + "',"
        sqlstr += "'" + ZE + "',"
        sqlstr += "'" + RQ + "',"
        sqlstr += "'" + BZ + "'"
        sqlstr += ")"
        SQLupdata(sqlstr)

        Dim msgstr As String = ""
        msgstr += "品牌型号：" + PP + "  " + XH + vbCrLf
        msgstr += "入货单价：" + DJ + "元" + vbCrLf
        msgstr += "入货数量：" + SL + "台" + vbCrLf
        msgstr += "入货支出：" + ZE + "元" + vbCrLf + vbCrLf
        msgstr += "入货操作完成！"
        MessageBox.Show(msgstr, "操作成功", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        ComboBox1.Text = "请选择..."
        ComboBox2.Text = "请选择..."
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        NumericUpDown1.Value = 1
        ComboBox1.Focus()

    End Sub

End Class